package com.cxx.purchasecharge.service.auditlog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import com.cxx.purchasecharge.dal.auditlog.LogUtil;
import com.cxx.purchasecharge.dal.common.GenericDaoUtils;

public class LogManagementAspectImpl implements LogManagementAspect
{
    private static final Logger LOGGER = Logger.getLogger (LogManagementAspectImpl.class);
    private LogUtil logUtil;

    /**
     * for delete
     * 
     * @param joinpoint delete case always has id
     */
    public void before (JoinPoint joinpoint)
    {
        Object[] arguments = joinpoint.getArgs ();
        if (null != arguments && arguments.length == 2)
        {
            Object obj = arguments[0];
            String user = arguments[1] != null ? arguments[1].toString () : "";
            logUtil.logForDelete (obj, user);
            LOGGER.info ("logUtil.logForDelete is called.");
        }
    }

    // for update
    public void around (ProceedingJoinPoint joinpoint) throws Throwable
    {
        Object[] arguments = joinpoint.getArgs ();
        if (null != arguments && arguments.length == 2)
        {
            Object obj = arguments[0];
            long id = (Long) GenericDaoUtils.getPrimaryKey (obj);
            Object source = logUtil.getDBModel (obj.getClass (), id);
            Object target = joinpoint.proceed ();
            String user = arguments[1] != null ? arguments[1].toString () : "";
            logUtil.logForUpdate (source, target, user);
            LOGGER.info ("logUtil.logForUpdate is called.");

            // Object idObj = ModelClassHelper.getFieldValueByName ("id", obj);
            // if (null != idObj && !"0".equals (idObj.toString ()))
            // {
            // Object target = joinpoint.proceed ();
            // logUtil.logForUpdate (obj, target, "");
            // LOGGER.info ("logUtil.logForUpdate is called.");
            // }
        }
    }

    // for add
    public void after (JoinPoint joinpoint)
    {
        Object[] arguments = joinpoint.getArgs ();
        if (null != arguments && arguments.length == 2)
        {
            Object obj = arguments[0];
            String user = arguments[1] != null ? arguments[1].toString () : "";
            logUtil.logForInsert (obj, user);
            LOGGER.info ("logUtil.logForInsert is called.");

            // Object idObj = ModelClassHelper.getFieldValueByName ("id", obj);
            // if(null != idObj && "0".equals (idObj.toString ()))
            // {
            // logUtil.logForInsert (obj, "");
            // LOGGER.info ("logUtil.logForInsert is called.");
            // }
        }
    }

    @Override
    public void login (JoinPoint joinpoint)
    {
        Object[] arguments = joinpoint.getArgs ();
        if (null != arguments && arguments.length == 2 && null != arguments[0])
        {
            logUtil.logForLog (arguments[0].toString (), convert (arguments[1]), true);
        }
    }

    @Override
    public void logout (JoinPoint joinpoint)
    {
        Object[] arguments = joinpoint.getArgs ();
        if (null != arguments && arguments.length == 1 && null != arguments[0])
        {
            logUtil.logForLog (arguments[0].toString (), null, false);
        }
    }

    private List <String> convert (Object obj)
    {
        List <String> strList = new ArrayList <String> ();
        if (obj instanceof java.util.Collection)
        {
            Iterator iter = ((java.util.Collection) obj).iterator ();
            while (iter.hasNext ())
            {
                strList.add (iter.next ().toString ());
            }
        }
        return strList;
    }

    public LogUtil getLogUtil ()
    {
        return logUtil;
    }

    public void setLogUtil (LogUtil logUtil)
    {
        this.logUtil = logUtil;
    }

}
